<%@ page contentType="text/html;charset=UTF-8" language="java"
	pageEncoding="utf-8"%>
<%@ include file="../include/head.jsp"%>
<%@ include file="../include/top.jsp"%>
<%@ include file="../include/left.jsp"%>
<div class="main-content">

	<%@ include file="../include/navigate.jsp" %>
	
	<div class="container">
		<div id="saveStatus"></div>
		
		<div class="widget-box">
			<div class="widget-box-header">
				<div class="col-sm-9">
					<div class="title">日志列表</div>
				</div>
				<div class="col-sm-3" style="margin-top: 2px; text-align: right;">
					<div class="btn-group-sm" style="padding: 2px;">
						<button type="button" class="btn btn-default btn-sm" onclick="" id="clearOptions" data-toggle="dropdown">清空日志</button> 
						<ul class="dropdown-menu" role="menu" aria-labelledby="clearOptions">
							<li role="presentation">
								<a role="menuitem" tabindex="-1" href="">清空一周前所有日志</a>
							</li>
							<li role="presentation" class="divider"></li>
							<li role="presentation">
								<a role="menuitem" tabindex="-1" href="">清空一个月前所有日志</a>
							</li>
						</ul>
						<button type="button" class="btn btn-danger btn-sm" onclick="openModal('${pageContext.request.contextPath}/cronJob/forceStopAll','确定停止？','是否强制停止所有正在执行的任务？')">停止所有任务</button>
					</div>
				</div>
			</div>
			
			<div class="widget-box-content" style="padding: 15px;">
				<div class="dataTables_wrapper form-inline">
					<div class="table-responsive">
						<table class="table table-hover">
							<thead>
								<tr>
									<th>作业名称</th>
									<th>执行用户</th>
									<th>开始时间</th>
									<th>结束时间</th>
									<th>执行状态</th>
									<th>插入条数</th>
									<th>查看日志</th>
								</tr>
							</thead>
							<tbody>
								<c:forEach items="${list}" var="bean">
									<c:set var="isRunning" value="${runningJobAddress[bean.executorThread] != null}"></c:set>
									<c:set var="trClass" value="${isRunning == 'true'? 'info':''}"></c:set>
									<tr class="${trClass}" jobId="${bean.id}"> 
										<td><kbd>${bean.jobName}</kbd></td>
										<td><kbd>${bean.username}</kbd></td>
										<td><fmt:formatDate value="${bean.beginTime}" pattern="yyyy-MM-dd HH:mm:ss" /></td>
										<td><fmt:formatDate value="${bean.endTime}" pattern="yyyy-MM-dd HH:mm:ss" /></td>
										<td>${bean.status}</td>
										<td>${bean.nbLine}</td>
										<td>
											<button type="button" class="btn btn-default"
												onclick="showMessage(this)" data-container="body" jobId='${bean.id}' isRunning='${isRunning}'
												data-placement="left" data-content="${bean.message}">查看详细
											</button>
										</td>
									</tr>
								</c:forEach>
							</tbody>
						</table>
					</div>
				</div>
			</div>
		</div>
		<div class="row clearfix ">
			<div class="col-md-12" align="center">
				<nav>
					<ul class="pagination pagination-sm">
					</ul>
				</nav>
			</div>
		</div>
	</div>

	<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	    <div class="modal-dialog">
	        <div class="modal-content">
	            <div class="modal-header">
	                <button type="button" class="close" data-dismiss="modal"><span
	                        aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
	                <h4 class="modal-title" id="myModalLabel">查看详细</h4>
	            </div>
	            
	            <div class="modal-body" id="modalbodyvalue" style=" height: 500px; overflow-y:scroll;">
	            </div>
	
	            <div class="modal-footer">
	            	<c:if test="${sessionScope.validUrls.contains('/cronJob/forceStop')}">
	                	<button jobId="" id="stopJobButton" onclick="forceStopJob();" type="button" class="btn btn-danger" data-clipboard-target="modalbodyvalue">强制停止</button>
	                </c:if>
	                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
	            </div>
	        </div>
	    </div>
	</div>

	<div style="position: fixed; bottom: 0px; width: 100%;" align="center">
		<%@ include file="../include/footer.jsp"%>
	</div>
</div>
<script type="text/javascript">
    function showMessage(detailLogButton) {
    	
    	if('true' == $(detailLogButton).attr("isRunning")){
    		$("#stopJobButton").show();
    		$("#stopJobButton").attr('jobId', $(detailLogButton).attr("jobId"));
    	} else {
    		$("#stopJobButton").hide();
    	}
        
        $("#modalbodyvalue").html($(detailLogButton).attr("data-content"));
        // $("#modalbodyvalue").html("<pre>" + $(detailLogButton).attr("data-content") + "</pre>");
        $('#myModal').modal('show');
    }
    
    function forceStopJob(){
    	var jobId = $("#stopJobButton").attr('jobId');
    	$.ajax({  
        	type:'post',
            url: '/cronJob/forceStop',
            data: {"jobId": jobId},  
            async: false,
            cache:false,    
            dataType:'json',    
            success: function(data) {
            	$('#myModal').modal('hide');
            	if('success' == data){
            		operationSuccess();
            		window.location='${pageContext.request.contextPath}/log/list/' + ${pageInfo.pageNum};
            	} else {
            		operationFailedWithMessage(message);
            	}
            },    
            error : function() {
            	$('#myModal').modal('hide');
            	operationFailed();
            }
        });
    }
    
    $('.pagination').twbsPagination({
        totalPages: ${pageInfo.pages},
        visiblePages: 5,
        href: '/log/list/{{number}}'
    });
</script>
<%@ include file="../include/bottom.jsp"%>